<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of counter_model
 *
 * @author hoabui
 */
class counter_model extends CI_Model {

    //put your code here
    function __construct() {
        parent::__construct();
    }

    function getOnlines($session, $time) {



        $myFile = "counter/online.db";

        $fr = fopen($myFile, 'r');

        $length;
        $onlines = array();
        $i = 0;
        $index = -1;
        $count = 0;
        while ($line = fgets($fr)) {
            $line = trim($line);
            if (strlen($line) >= 1) {
                $strs = explode("-", $line);
                if ($strs[0] == $session) {
                    $count++;
                    $index = $i;
                }
                array_push($onlines, $line);
                $i++;
            }
        }
        $length = count($onlines);
        if ($count == 0) {
            $onlines[$i + 1] = $session . '-' . $time;
        } else {
            $onlines[$index] = $session . '-' . $time;
        }

        if ($onlines != NULL) {
            for ($j = $length - 1; $j > 0; $j--) {
                $strs = explode("-", $onlines[$j]);
                $t = (int) $strs[1];
                if ($t < $time - 50) {
                    $l = $length - 1;
                    $onlines[$j] = $onlines[$l];
//                    array_diff($onlines, $t);
                    array_pop($onlines);
                }
            }
        }

        $count_user = count($onlines);



        fclose($fr);

        $fh = fopen($myFile, 'w') or die("can't open file");
        foreach ($onlines as $value) {
            $value = trim($value);
            if ($value != "")
                fwrite($fh, $value . "\n");
        }

        fclose($fh);

        return $count_user;

//
//        $query = $this->db->get_where(tbl_online, array('session' => $session));
//        $count = $query->num_rows();
//        if ($count == NULL) {
//            $data = array('session' => $session, 'time' => $time);
//            $this->db->insert(tbl_online, $data);
//        } else {
//            $data = array('time' => $time);
//            $this->db->where('session', $session);
//            $this->db->update(tbl_online, $data);
//        }
//
//        $query = $this->db->get(tbl_online);
//        $count_user = $query->num_rows();
//
//
//        $this->db->where('time <', $time - 10);
//        $this->db->delete(tbl_online);
//
//        return $count_user;
    }

    function getAllCounter() {
        $myFile = "counter/counter.db";

        $fr = fopen($myFile, 'r');

        $count = 0;

        while ($line = fgets($fr)) {
            $line = trim($line);
            if (strlen($line) >= 1) {
                $strs = explode("-", $line);
                $count++;
            }
        }
        fclose($fr);
//        $query = $this->db->get(tbl_counter);
//        return $query->num_rows();
        return $count;
    }

    function getCounter($ip, $date) {
        $myFile = "counter/counter.db";

        $fr = fopen($myFile, 'r');

        $count = 0;

        while ($line = fgets($fr)) {
            $line = trim($line);
            if (strlen($line) >= 1) {
                $strs = explode("-", $line);
                if ($strs[1] == $date) {
                    $count++;
                }
            }
        }

        fclose($fr);
        return $count;
//        $query = $this->db->get_where(tbl_counter, array('ip' => $ip, 'date' => $date));
//        return $query->num_rows();
    }

    function addCounter($ip, $date) {


        $myFile = "counter/counter.db";

        $fr = fopen($myFile, 'r');

        $length;
        $onlines = array();

        $index = -1;
        $count = 0;
        $flag = false;
        while ($line = fgets($fr)) {
            $line = trim($line);
            if (strlen($line) >= 1) {
                $strs = explode("-", $line);
                if ($strs[0] == $ip && $strs[1] == $date) {
                    $flag = true;
                }
                array_push($onlines, $line);
            }
        }
        $length = count($onlines);
        fclose($fr);


        if (!$flag) {
            $counter = $ip . "-" . $date;
            array_push($onlines, $counter);
        }
        
        
        $fh = fopen($myFile, 'w') or die("can't open file");
        foreach ($onlines as $value) {
            $value = trim($value);
            if ($value != "")
                fwrite($fh, $value . "\n");
        }

        fclose($fh);

    
//        $query = $this->db->get_where(tbl_counter, array('ip' => $ip, 'date' => $date));
//        $count = $query->num_rows();
//        if ($count == NULL) {
//            $data = array('ip' => $ip, 'date' => $date);
//            $this->db->insert(tbl_counter, $data);
//        }
    }

}

?>
